<?php

namespace app\models;

/**
 * 购物车
 * Class ShoppingCart
 * @package app\models
 */
class Cart
{
    public static function getCart($userId)
    {
        $cmd = \Yii::$app->db->createCommand('
            SELECT t0.id, t0.goods_code, t0.quantity, t0.is_selected, t2.`name`, t2.selling_price,t2.selling_price*t3.integral_rate AS selling_integral,t3.integral_unit,t2.selling_price*t3.integral_rate*t0.quantity AS selling_integral_sum,t4.image_url AS goods_image FROM t_cart t0
            LEFT JOIN t_user t1 ON t1.id=t0.user_id
            LEFT JOIN t_goods t2 ON t2.institution_code=t1.institution_code AND t2.code=t0.goods_code
            LEFT JOIN t_institution t3 ON t3.`code`=t1.institution_code
            LEFT JOIN t_goods_image t4 ON t4.goods_code=t2.`code` AND t4.type=1
            WHERE t2.is_enabled=1 AND t2.is_shelved=1 AND t2.is_show=1 AND t0.user_id=:user_id');
        $cmd->bindValue(':user_id', $userId);

        $items = $cmd->queryAll();
        return $items;
    }

    public static function addCart($userId, $goodsCode, $quantity)
    {
        $cmd = \Yii::$app->db->createCommand('CALL p_add_cart(:in_user_id, :in_goods_code, :in_quantity)');
        $cmd->bindValue(':in_user_id', $userId);
        $cmd->bindValue(':in_goods_code', $goodsCode);
        $cmd->bindValue(':in_quantity', $quantity);

        $cartId = $cmd->queryScalar();
        return $cartId;
    }

    public static function setCartQuantity($userId, $cartId, $quantity)
    {
        $cmd = \Yii::$app->db->createCommand('CALL p_set_cart_quantity(:in_user_id, :in_cart_id, :in_quantity)');
        $cmd->bindValue(':in_user_id', $userId);
        $cmd->bindValue(':in_cart_id', $cartId);
        $cmd->bindValue(':in_quantity', $quantity);

        $cmd->execute();
    }

    public static function removeCart($userId, $cartId)
    {
        $cmd = \Yii::$app->db->createCommand('CALL p_remove_cart(:in_user_id, :in_cart_id)');
        $cmd->bindValue(':in_user_id', $userId);
        $cmd->bindValue(':in_cart_id', $cartId);

        $cmd->execute();
    }

    public static function setCartSelected($userId, $cartId, $isSelected)
    {
        $cmd = \Yii::$app->db->createCommand('CALL p_set_cart_selected(:in_user_id, :in_cart_id, :in_is_selected)');
        $cmd->bindValue(':in_user_id', $userId);
        $cmd->bindValue(':in_cart_id', $cartId);
        $cmd->bindValue(':in_is_selected', $isSelected);

        $cmd->execute();
    }

    public static function getCartSelected($userId)
    {
        $cmd = \Yii::$app->db->createCommand('
            SELECT t0.id, t0.goods_code, t0.quantity, t0.is_selected, t2.`name`, t2.is_crossborder, t2.selling_price,t2.selling_price*t3.integral_rate AS selling_integral,t2.selling_price*t3.integral_rate*t0.quantity AS selling_integral_sum,t4.image_url AS goods_image FROM t_cart t0
            LEFT JOIN t_user t1 ON t1.id=t0.user_id
            LEFT JOIN t_goods t2 ON t2.institution_code=t1.institution_code AND t2.code=t0.goods_code
            LEFT JOIN t_institution t3 ON t3.`code`=t1.institution_code
            LEFT JOIN t_goods_image t4 ON t4.goods_code=t2.`code` AND t4.type=1
            WHERE t2.is_enabled=1 AND t2.is_shelved=1 AND t2.is_show=1 AND t0.is_selected=1 AND t0.user_id=:user_id');
        $cmd->bindValue(':user_id', $userId);

        $items = $cmd->queryAll();
        return $items;
    }
}